﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Sockets;
using System.IO;
namespace WFWS.Trying
{
    class Tcp
    {
        const int _bufferSize = 128;
       byte[] _buffer = new byte[_bufferSize];
       private int _index ;
        TcpListener _listener = new TcpListener(888);
        NetworkStream _stream;
        public void Go()
        {
            _listener.Start();
            _listener.BeginAcceptSocket(Callback, null);
        }

        private void Callback(IAsyncResult ar)
        {
            var socket = _listener.EndAcceptSocket(ar);
            _stream = new NetworkStream(socket,true);
            _stream.BeginRead(_buffer, 0, _bufferSize, OnRead,null);
        }

        private void OnRead(IAsyncResult ar)
        {
            var bytes = _stream.EndRead(ar);
            
            string temp = Encoding.ASCII.GetString(_buffer, 0, bytes);

            File.AppendAllText("e:\\log.txt", temp + "\r\n\r\n");
            _stream.BeginRead(_buffer, 0, _bufferSize, OnRead, null);
        }
        
    }
}
